//
//  ZYZNewFeatureController.swift
//  MyWeiBo
//
//  Created by 牛牛很牛 on 15/12/17.
//  Copyright © 2015年 曾勇召. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class ZYZNewFeatureController: UICollectionViewController {
    
    private let itemCount = 4

    let layout  =  UICollectionViewFlowLayout()

    //Appdelegate回调用没有参数的构造函数
    init() {
    super.init(collectionViewLayout: layout)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
       self.prepareCollectionView()
    }
    
    private func prepareCollectionView(){
        self.collectionView!.registerClass(ZYZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        //不要间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false

    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ZYZNewFeatureCell
       cell.index = indexPath.item
        return cell
    }
    
    //cell停到某一页显示
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //取出停止后的indexPath
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        if visibleIndexPath.item == itemCount - 1{
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! ZYZNewFeatureCell
            cell.startButtonAnimation()
        }
    }
    
    
    //切换控制器
//     func strartButtonClick() {
//        // (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
//      
//        self.presentViewController(UINavigationController(rootViewController: ZYZHomeController()), animated: true) { () -> Void in
//        }
//    }

}


/*
class ZYZNewFeatureCell : UICollectionViewCell {
    
 
    //系统自动调用
     override init(frame: CGRect) {
        super.init(frame:frame)
        prepareUI()
        
    }
     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
    
    //定义一个分页index
    var index = 0 {
        didSet{//index属性发生改变时，就调用
            bgImageView.image = UIImage(named: "new_feature_\(index+1)")
            startButton.hidden = true
        }
    }
    
    //设置按钮动画
    
    func strartButtonAnimation(){
    self.startButton.hidden = false
        self.startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                //self.startButton.transform = CGAffineTransformMakeScale(1, 1)
                print("动画完成")
        }
    }

    
    private func prepareUI(){
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        //约束bgimageView
        contentView.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: contentView, attribute: .Top, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: .Equal, toItem: contentView, attribute: .Leading, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: contentView, attribute: .Bottom, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: bgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: .Equal, toItem: contentView, attribute: .Trailing, multiplier: 1, constant: 0))
    
        //约束startButton
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: .Equal, toItem: contentView, attribute: .Bottom, multiplier: 1, constant: -160))
        
    }
    
//    //切换控制器
    private func strartButtonC() {
        print("333")
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
    }
    
    //MARK: - CELL懒加载
    //背景图
    private lazy var bgImageView :UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    //按钮
    private lazy var startButton :UIButton = {
      let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        //添加点击事件切换控制器
        button.addTarget(self, action: "strartButtonC", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    





}*/

// MARK: - 自定义cell显示新特性界面
class ZYZNewFeatureCell: UICollectionViewCell {
    
    // MARK: - 属性
    // 不同的cell显示不同的图片,别人告诉我们显示哪一页的图片
    // 根据不同的index来设置不同的图片
    var index = 0 { // 0 - 3
        // 属性监视器, 当index发生变化时会调用
        didSet {
            // 找对对应的图片,设置给背景视图
            bkgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            
            // 每当cell要显示之前都隐藏开始按钮
            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 系统会来调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    // MARK: - 开始按钮动画
    func startButtonAnimation() {
        // 显示按钮
        startButton.hidden = false
        
        // 设置开始按钮的scale
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        // 使用UIView动画
        UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 将开始按钮的transform的缩放设置成1(x,y)
            //            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            // 清空transform
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("开始按钮动画完成")
        }
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 添加子控件, TableViewCell和CollectionViewCell,添加子控件需要添加到contentView上面
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startButton)
        
        // 添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景填充父控件
        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 开始体验按钮约束
        // 水平CenterX和父控件的CenterX重合
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 垂直开始按钮的底部和父控件的底部重合
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    
    // MARK: - 点击事件
    func startButtonClick() {
        // 调用AppDelegate来切换根控制器,不能直接创建AppDelegate(),需要使用系统维护的AppDelegate
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
    }
    
    // MARK: - 懒加载, 按钮会自动适配图片的大小
    /// 背景图片.
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    /// 开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置按钮文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        // 因为cell复用,不能再创建的时候来隐藏cell
        //        button.hidden = true
        
        // 按钮添加点击事件
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}


















